<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_article_gallery}}".
 *
 * @property int $id
 * @property int $article_id 文章ID
 * @property string $path 原图地址
 * @property int $attach_id 附件ID
 * @property string|null $description 图片描述
 * @property int $listorder 显示排序
 * @property int $width 图片宽度
 * @property int $height 图片高度
 * @property int $size 图片大小
 * @property int $created_at
 * @property int $updated_at
 */
class CmsArticleGallery extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_article_gallery}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['article_id', 'attach_id', 'listorder', 'width', 'height', 'size', 'created_at', 'updated_at'], 'integer'],
            [['path', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'article_id' => '文章ID',
            'path' => '原图地址',
            'attach_id' => '附件ID',
            'description' => '图片描述',
            'listorder' => '显示排序',
            'width' => '图片宽度',
            'height' => '图片高度',
            'size' => '图片大小',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * 关联文章详情
     */
    public function getArticle() {
        return $this->hasOne(CmsArticle::class, ['id' => 'article_id']);
    }

    /**
     * 关联文章详情
     */
    public function getSource() {
        return Url::attach($this->path);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'path' => 'source',
            'description',
            'format' => function() {
                return 'image';
            },
        ];
    }

}
